<?php

class Admin_Admin_Controller extends Base_Controller {


	public function action_index()
	{
		return View::make('admin/admin');
	}

	public function action_getdata()
    {
        $factorPgData = DB::table('users_admin')
        		->where('dele','=',1)
                ->paginate(10);

        foreach($factorPgData->results as $k =>$v){
          $factorPgData->results[$k]['addtime']=date('Y-m-d H:i:s',$v['addtime']);
    };
        $res = array(
            'code'=>0,
            'msg'=>'',
            'count'=>$factorPgData->total,
            'data'=>$factorPgData->results,
            );
        return Response::json($res); //把数据转化成json返回
    }


    public function action_adminuser()
    {
    	$id = Input::get('id');
    	$str =DB::table('users_admin')
    			->where('id','=',$id)->first();
    	
    	return View::make('admin/admindatil',array('str'=>$str));
    }

	public function action_dele()
	{
		$id = Input::get('id');
        $dele = -1;
        $str = DB::table('users_admin')->where('id','=',$id)->update(array('dele'=>$dele));
        if($str){
            $data = array(
                'code'=>'200',
                'msg'=>'删除成功'
                );
        }else{
            $data = array(
                'code'=>'400',
                'msg'=>'删除失败'
                );
        }

        return json_encode($data);

	}

	public function action_changeadmin()
	{

		$id = Input::get('id');
		$res = DB::table('users_admin')
				->where('id','=',$id)
				->first();
		return 	View::make('admin/changeadmin',array('res'=>$res));

	}

	public function action_dochange()
	{
		$id = Input::get('id');
		$username = Input::get('username');
		$password = Input::get('password');
		$time= time();
		$str = DB::table('users_admin')
					->where('id','=',$id)
					->update(array('username'=>$username,'password'=>$password,'addtime'=>$time));
        if($str){
            return View::make('jump/success',array('msg'=>'修改成功,','time'=>1));
        }else{
            return View::make('jump/success',array('msg'=>'修改失败','time'=>2));
        }
	}


    public function action_addadmin()
    {
        return View::make('admin/addadmin');

    }

    public function action_doaddoper(){
        $username = Input::get('username');
        $password = Input::get('password');
        $addtime = time();
        $code = 0;
        $str1 = DB::table('users_admin')->where('username','=',$username)->first();
        if($str1){
            return json_encode(array('code'=>'40','msg'=>'账号已存在'));
        }else{
            $str = DB::table('users_admin')->insert(array('username'=>$username,'password'=>$password,'addtime'=>$addtime,'code'=>$code));
            if($str){
                return json_encode(array('code'=>'20','msg'=>'添加成功'));
            }else{
                return json_encode(array('code'=>'40','msg'=>'添加失败'));
            }
        }




    }


































}